<?php

namespace App\Orchid\Screens\Tag;

use App\Models\Tag;
use Orchid\Screen\Screen;
use Orchid\Screen\Actions\Link;
use Orchid\Support\Facades\Toast;
use Orchid\Support\Facades\Layout;
use Orchid\Screen\TD;

class TagListScreen extends Screen
{
    /**
     * Fetch data to be displayed on the screen.
     *
     * @return array
     */
    public function query(): iterable
    {
        return [
            'tags' => Tag::withCount('posts')
                ->orderBy('name')
                ->paginate(),
        ];
    }

    /**
     * The name of the screen displayed in the header.
     */
    public function name(): ?string
    {
        return '标签管理';
    }

    /**
     * Display header description.
     */
    public function description(): ?string
    {
        return '管理所有文章标签';
    }

    /**
     * The screen's action buttons.
     *
     * @return \Orchid\Screen\Action[]
     */
    public function commandBar(): iterable
    {
        return [
            Link::make('新建标签')
                ->icon('plus')
                ->route('platform.tags.create'),
        ];
    }

    /**
     * The screen's layout elements.
     *
     * @return \Orchid\Screen\Layout[]
     */
    public function layout(): iterable
    {
        return [
            Layout::table('tags', [
                TD::make('name', '标签名称')
                    ->sort()
                    ->filter(TD::FILTER_TEXT)
                    ->render(function (Tag $tag) {
                        return Link::make($tag->name)
                            ->route('platform.tags.edit', $tag);
                    }),
                    
                TD::make('slug', 'Slug')
                    ->sort()
                    ->render(fn (Tag $tag) => $tag->slug),
                    
                TD::make('posts_count', '文章数量')
                    ->sort()
                    ->render(function (Tag $tag) {
                        return $tag->posts_count;
                    }),
                    
                TD::make('created_at', '创建时间')
                    ->sort()
                    ->render(function (Tag $tag) {
                        return $tag->created_at->format('Y-m-d H:i');
                    }),
                    
                TD::make('操作')
                    ->align(TD::ALIGN_CENTER)
                    ->width('150px')
                    ->render(function (Tag $tag) {
                        return Link::make('编辑')
                            ->icon('pencil')
                            ->route('platform.tags.edit', $tag);
                    }),
            ]),
        ];
    }
    
    /**
     * Remove tag
     *
     * @param Tag $tag
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function remove(Tag $tag)
    {
        // 检查是否有关联的文章
        if ($tag->posts()->count() > 0) {
            Toast::error('无法删除包含文章的标签');
            return;
        }
        
        $tag->delete();
        
        Toast::info('标签已删除');
    }
}